# -*- coding: utf-8 -*-
"""
Created on Tue Mar 11 15:19:22 2025

@author: jingla
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.signal import savgol_filter

from AuxiliaryFunctions import (plot_scope_signal_two_curves,
                                Ic_and_efficiency_diode_1file_V2,
                                plot_scope_signal_final)
font = {'family' : 'DejaVu Sans',
        'weight' : 'normal',
        'size'   : '15'}
plt.rc('font', **font)
path = ".\\DataRectifier1\\"

#%%
#%%
#%% Hz-dependence critical currents
#%% Bz dependence R2: 1I-1I+
B0 = -1.6
Vran = []
filename = 'M16_VPT0112_Ch1_IV_I+I-_Vmaxpm3V_10kOhm_Bz-m40to20Oe__1p7K_FixedHolder_AfterBzm1T.dat'

Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\mu_0 H_z$ (mT)',
                              B0=B0, Bran=[-2, 2], Vran=Vran,
                              title='M16 R2: 1I+ to 1I- after Bz=-1 T',
                              rs=0.6, savefig=True)#-100, 100

#%% Bz dependence R2: 1I-1I+
B0 = (17 + 27) / 20
Vran = []
filename = 'M41_VPT0112_Ch1_IV_I+I-_Vmaxpm3V_10kOhm_Bz-m10to50Oe_1p7K_FixedHolder.dat'
Ic_and_efficiency_diode_1file_V2(path+filename, plot_IV_curve=False, B_dir='$\mu_0H_z$ (mT)',
                              B0=B0, Bran=[-2, 2], Vran=Vran, title='R2: 1I+ to 1I- after Bz=1 T', rs=0.6, savefig=True)#-100, 100

#%%
#%% IVs rectifier Fig. 1
#%% M18 + M42, IVs with opposite polarities
import matplotlib.lines as mlines
filename = ['M18_VPT0112_Ch1_IV_I+I-_Bz-m14Oe__1p7K_FixedHolder_AfterBzm1TReSaturatedm2kOe.dat',
            'M42_VPT0112_Ch1_IV_I+I-_Vmaxpm3V_10kOhm_Bz-24Oe_1p7K_FixedHolder.dat']

#fig, ax1 = plt.subplots(figsize=(4.5, 3.5)) # 
fig, ax1 = plt.subplots(figsize=(4, 3.75))
for i, f in enumerate(filename):
    Vin_V, Is_mA, Vs_mV, dIs_V,	time = np.loadtxt(path+f, skiprows=1).T
    filt_dV = np.abs(np.diff(Vs_mV,append=0))>50
    print((Is_mA[filt_dV]))
    if i == 0:
        plt.plot(Vs_mV[:500], Is_mA[:500], c='C{}'.format(i + 2))
    else:
        plt.plot(Vs_mV[:600], Is_mA[:600], c='C{}'.format(i + 2))
plt.minorticks_on()
plt.tick_params(which='both', direction='in', top=True, right=True)
plt.xlabel('$V$ (mV)', fontsize=20)
plt.ylabel('$I$ (mA)', fontsize=20)
blue_line = mlines.Line2D([], [], color='C2', linestyle='-', linewidth=2, label='$\mu_0H_z\\approx -$0.4 mT')
orange_line = mlines.Line2D([], [], color='C3', linestyle='-', linewidth=2, label='$\mu_0H_z\\approx +$0.4 mT')
plt.legend(handles=[blue_line, orange_line])
plt.title('M18 and M42')
plt.savefig('IVsRectifier.pdf')
plt.show()
#%%
#%%
#%%
#%% M26 and M44 for Reversible Rectification

filename1 = 'M26_VPT0112_Ch1_IV_I+I-_Vmaxpm1p828V_1kOhm_Bz-m14Oe__1p7K_FixedHolder_AfterBzm1Tfreq 36.81 Hz.dat'
filename2 = 'M44_VPT0112_Ch1_I_V+V-V_I+I-2Ohm_Vmaxpm1p688V_1kOhm_Bz-p24Oe__1p7K_FixedHolderfreq 36.81 Hz.dat'
plot_scope_signal_two_curves(path+filename1, path+filename2, title='', xlim=[0, 60], dt1=100, dt2=100 + 1.5 - 27.166*5) #-120, -50
plt.savefig('ReversibleRectifierOperation.pdf')
plt.show()
#%%
#%% R-dependence Rectifier signal
#%% 2 Ohm
filename = 'M44_VPT0112_Ch1_I_V+V-V_I+I-2Ohm_Vmaxpm1p688V_1kOhm_Bz-p24Oe__1p7K_FixedHolderfreq 36.81 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M44 f=37 Hz', xlim=[], dt=-2) #0, 50
plt.savefig('M44_2_Ohm.pdf')
plt.show()
#%% 6 Ohm
filename = 'M88_VPT0112_Ch1_I_V+V-V_I+I-6OhmResBox_Vmaxpm1p628V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M88 f=37 Hz', xlim=[], dt=8, max_ref=5e-3, max_filt=5.5e-3) #0, 50
plt.savefig('M88_6_Ohm.pdf')
plt.show()
#%% 10 Ohm
filename = 'M92_VPT0112_Ch1_I_V+V-V_I+I-10OhmResBox_Vmaxpm1p550V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M92 f=37 Hz', xlim=[], dt=103, max_ref=4e-3, max_filt=10e-3) #0, 50
plt.savefig('M92_10_Ohm.pdf')
plt.show()
#%% 20 Ohm
filename = 'M90_VPT0112_Ch1_I_V+V-V_I+I-20OhmResBox_Vmaxpm1p628V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M90 f=37 Hz', xlim=[], dt=10, max_ref=4e-3, max_filt=19e-3) #0, 50
plt.savefig('M90_20_Ohm.pdf')
plt.show()
plot_scope_signal_final(path+filename, title='M90 f=37 Hz', xlim=[], dt=10, max_ref=20e-3, max_filt=40e-3)
plt.show()
plot_scope_signal_final(path+filename, title='M90 f=37 Hz', xlim=[], dt=10, max_ref=10e-3, max_filt=25e-3)
plt.show()
#%% 50 Ohm
filename = 'M93_VPT0112_Ch1_I_V+V-V_I+I-50OhmResBox_Vmaxpm1p550V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M93 Bz= +24 Oe', xlim=[0, 50], dt = -18, max_ref=20e-3, max_filt=40e-3) #0, 50
plt.savefig('M93_50_Ohm.pdf')
plt.show()
plot_scope_signal_final(path+filename, title='M93 Bz= +24 Oe', xlim=[], dt = -18, max_ref=40e-3, max_filt=100e-3) #0, 50
plt.show()
plot_scope_signal_final(path+filename, title='M93 Bz= +24 Oe', xlim=[], dt = -18, max_ref=20e-3, max_filt=40e-3) #0, 50
plt.show()
#%% 100 Ohm
filename = 'M94_VPT0112_Ch1_I_V+V-V_I+I-100OhmResBox_Vmaxpm1p550V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M94 Bz= +24 Oe', xlim=[0,50], dt = 120, max_ref=40e-3, max_filt=200e-3) #0, 50
plt.savefig('M94_100_Ohm.pdf')
plt.show()
plt.figure(figsize=[7.2,2.5])
plot_scope_signal_final(path+filename, title='M94 Bz= +24 Oe', xlim=[0,320],tzero=True, dt = 0,
                  max_ref=60e-3, max_filt=200e-3)
plt.savefig('M94_100_Ohm_fullrange.pdf')
plt.show()
plt.figure(figsize=[7.2,2.5])
plot_scope_signal_final(path+filename, title='M94 Bz= +24 Oe', xlim=[0,320],tzero=True, dt = 0,
                  max_ref=40e-3, max_filt=80e-3)
#plt.savefig('M94_100_Ohm_fullrange.pdf')
plt.show()
#%% 200 Ohm
filename = 'M96_VPT0112_Ch1_I_V+V-V_I+I-200OhmResBox_Vmaxpm1p550V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M96 Bz= +24 Oe', xlim=[], dt = 22, max_ref=50e-3, max_filt=90e-3) #0, 50
plt.savefig('M96_200_Ohm.pdf')
plt.show()
plt.figure(figsize=[6,2.5])
plot_scope_signal_final(path+filename, title='M96 Bz= +24 Oe', xlim=[], dt = -4, max_ref=80e-3, max_filt=200e-3)
plt.plot([-100, 100], [70, 70],'k')
plt.show()
plt.figure(figsize=[6,2.5])
plot_scope_signal_final(path+filename, title='M96 Bz= +24 Oe', xlim=[], dt = -4, max_ref=80e-3, max_filt=200e-3)
plt.plot([-100, 100], [70, 70],'k')
plt.show()
#%% 300 Ohm
filename = 'M100_VPT0112_Ch1_I_V+V-V_I+I-300OhmResBox_Vmaxpm1p346V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M100 Bz= +24 Oe', xlim=[], dt = 22) #0, 50
plt.savefig('M100_300_Ohm.pdf')
plt.show()
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M100 Bz= +24 Oe', xlim=[], dt = 22) #0, 50
plt.show()
#%% 400 Ohm
filename = 'M105_VPT0112_Ch1_I_V+V-V_I+I-400OhmResBox_Vmaxpm1p346V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M105 Bz= +24 Oe', xlim=[], dt = 22) #0, 50
plt.savefig('M105_400_Ohm.pdf')
plt.show()
#%% 500 Ohm
filename = 'M104_VPT0112_Ch1_I_V+V-V_I+I-500OhmResBox_Vmaxpm1p346V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M104 Bz= +24 Oe', xlim=[], dt = 22) #0, 50
plt.savefig('M104_500_Ohm.pdf')
#%% 600 Ohm
filename = 'M103_VPT0112_Ch1_I_V+V-V_I+I-600OhmResBox_Vmaxpm1p346V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M103 Bz= +24 Oe', xlim=[], dt = 22) #0, 50
plt.savefig('M103_600_Ohm.pdf')
#%% 700 Ohm
filename = 'M104_VPT0112_Ch1_I_V+V-V_I+I-700OhmResBox_Vmaxpm1p346V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M104 Bz= +24 Oe', xlim=[], dt = 22) #0, 50
plt.savefig('M104_700_Ohm.pdf')
#%% 800 Ohm
filename = 'M103_VPT0112_Ch1_I_V+V-V_I+I-800OhmResBox_Vmaxpm1p346V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M103 Bz= +24 Oe', xlim=[], dt = -20) #0, 50
plt.savefig('M103_800_Ohm.pdf')
#%% 900 Ohm
filename = 'M102_VPT0112_Ch1_I_V+V-V_I+I-900OhmResBox_Vmaxpm1p346V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Standard1MHzFiltfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='M102 Bz= +24 Oe', xlim=[], dt = -5) #0, 50
plt.savefig('M102_900_Ohm.pdf')
#%%
#%% Overview resistance dependence
#%%
#%% Number of peaks per amplitude and resistance
matrix = np.array([
    [2, 1.78, 0.03, 24, 0, 0, 2.13, 0],
    [6, 5.2, 0.1, 19, 0, 0, 2.04, 0],
    [10, 8.08, 0.09, 21, 0, 0, 1.96, 0],
    [20, 15.1, 0.2, 33, 29.0, 0.8, 2, 14],
    [50, 32.7, 0.3, 9, 59, 1, 1.96, 15],
    [100, 51.7, 0.6, 3, 88, 5, 1.96, 21],
    [200, 70, 1, 0, 117, 6, 1.96, 23],
    [300, 0, 0, 0, 117.7, 0.9, 1.96, 23],
    [400, 0, 0, 0, 125.1, 0.9, 1.96, 23],
    [500, 0, 0, 0, 132, 3, 1.96, 23],
    [600, 0, 0, 0, 135, 2, 1.96, 22],
    [700, 0, 0, 0, 136, 2, 1.96, 23],
    [800, 0, 0, 0, 136.8, 0.6, 1.96, 22],
    [900, 0, 0, 0, 136.7, 0.8, 1.96, 23]
])
R,Vout1_mV,dVout_mV,n1,Vout2_mV,dVout2_mV,Imax,n2 = matrix.T
popt,pcov = curve_fit(lambda R, Rn: R/(2*R/Rn+1), R, Vout2_mV/Imax)
R_long = np.linspace(0, 900, 200)
plt.plot(R_long, R_long/(2*R_long/popt[0]+1), 'k')
#plt.plot(R_long, R_long/(3*R_long/popt[0]+2))
plt.errorbar(R, Vout2_mV/Imax, yerr=dVout2_mV/Imax, fmt='o',color='k')
plt.errorbar(R, Vout1_mV/Imax, yerr=dVout_mV/Imax, fmt='o',ecolor='black',
             markerfacecolor='white', markeredgecolor='black')
plt.tick_params(direction='in',top=True, right=True)
plt.title(f'Rn={int(round(popt[0]))}pm{int(round(np.sqrt(np.diag(pcov))[0]))} Ohm')
plt.xlabel('$R\,(\\Omega)$',fontsize=20)
plt.ylabel('$V_\\mathrm{out}^\\mathrm{max}/I_\\mathrm{in}^\\mathrm{max}\,(\\Omega)$',fontsize=20)  
plt.savefig('OutputVoltageVsR.pdf')  
plt.show()
plt.errorbar(R, Vout2_mV/(Imax * R), yerr=dVout2_mV/(Imax * R), fmt='o',color='k')
plt.errorbar(R, Vout1_mV/(Imax * R), yerr=dVout_mV/(Imax * R), fmt='o',ecolor='black',
             markerfacecolor='white', markeredgecolor='black')
plt.plot(R_long, 1/(2*R_long/popt[0]+1), 'k')
plt.plot(R_long, 1/(3*R_long/popt[0]+2), 'r')
plt.tick_params(direction='in',top=True, right=True)
plt.xlabel('$R\,(\\Omega)$',fontsize=20)
plt.ylabel('$I_\\mathrm{out}^\\mathrm{max}/I_\\mathrm{in}^\\mathrm{max}$',fontsize=20)  

plt.show()
plt.figure(figsize=[3,3.93])
plt.plot(R, n1, 'o', markerfacecolor='white', markeredgecolor='black')
plt.plot(R, n2, 'ok')
plt.minorticks_on()
plt.tick_params(direction='in', which='both',top=True, right=True)
plt.xlabel('$R\,(\\Omega)$',fontsize=20)
plt.ylabel('n. points',fontsize=20)
plt.xticks([0, 300, 600,900])
plt.savefig('numberDataPointsVsR.pdf')
plt.show()
#%% Capacitor and frequency (f) dependence
#%%
#%%
#%% time dependence f=50Hz
filename = 'M137_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.598V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_CapCir100uFfreq 50.16 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$f=50$ Hz $C=100\,\\mu$F',# xlim=[0, 60], ylim=[75, 125], # to reproduce SI figure
                  dt = 0, tzero=True, l_win=3, peak_sep=10, max_ref=8e-3,
                  max_filt=200e-3, rev=False)
#rev=True to find peaks and rev=False to find dips (with opposite sign) as Average Vmax
# the green line at the minima can be ignored and appears near the absolute minima of the data 
plt.savefig('M137_50Hz_100uF.pdf')
plt.show()
#%% time dependence f=100Hz
filename = 'M138_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.598V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_CapCir100uFfreq 100.16 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$f=100$ Hz $C=100\,\\mu$F', #xlim=[0, 40], ylim=[95, 120],# to reproduce SI figure
                        dt = 0, tzero=True, l_win=5, peak_sep=40, max_ref=8e-3,
                        max_filt=200e-3, rev=False)
#rev=True to find peaks and rev=False to find dips (with opposite sign) as Average Vmax
# the green line at the minima can be ignored and appears near the absolute minima of the data
plt.savefig('M138_100Hz_100uF.pdf')
plt.show()
#%% time dependence f=150Hz
filename = 'M139_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.598V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_CapCir100uFfreq 150.16 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$f=150$ Hz $C=100\,\\mu$F',# xlim=[0,20], ylim=[95, 117],# to reproduce SI figure
                        dt = 0, tzero=True, l_win=5, peak_sep=40, max_ref=8e-3,
                        max_filt=200e-3, rev=False)
#rev=True to find peaks and rev=False to find dips (with opposite sign) as Average Vmax
# the green line at the minima can be ignored and appears near the absolute minima of the data
plt.savefig('M139_150Hz_100uF.pdf')
plt.show()
#%% time dependence f=200Hz
filename = 'M140_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.598V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_CapCir100uFfreq 200.2 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$f=200$ Hz $C=100\,\\mu$F', #xlim=[0,15],ylim=[105, 117],# to reproduce SI figure
                        dt = 0, tzero=True, l_win=5, peak_sep=40, max_ref=5e-3,
                        max_filt=200e-3, rev=False)
#rev=True to find peaks and rev=False to find dips (with opposite sign) as Average Vmax
# the green line at the minima can be ignored and appears near the absolute minima of the data
plt.savefig('M140_200Hz_100uF.pdf')
plt.show()
#%% time dependence f=250Hz
filename = 'M141_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.598V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_CapCir100uFfreq 250.2 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$f=250$ Hz $C=100\,\\mu$F', #xlim=[0, 10], ylim=[105, 117],# to reproduce SI figure
                        dt = 0, tzero=True, l_win=5, peak_sep=40, max_ref=5e-3,
                        max_filt=200e-3, rev=False)
#rev=True to find peaks and rev=False to find dips (with opposite sign) as Average Vmax
# the green line at the minima can be ignored and appears near the absolute minima of the data
plt.savefig('M141_250Hz_100uF.pdf')
plt.show()
#%% time dependence f=300Hz
filename = 'M142_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.598V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_CapCir100uFfreq 300.2 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$f=300$ Hz $C=100\,\\mu$F',# xlim=[0, 10], ylim=[105, 117],# to reproduce SI figure
                        dt = 0, tzero=True, l_win=5, peak_sep=40, max_ref=4e-3,
                        max_filt=200e-3, rev=False) 
#rev=True to find peaks and rev=False to find dips (with opposite sign) as Average Vmax
# the green line at the minima can be ignored and appears near the absolute minima of the data
plt.savefig('M142_300Hz_100uF.pdf')
plt.show()
#%% time dependence f=10kHz
filename = 'M146_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.598V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCir100uFfreq 10016.0 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$f=10$ kHz $C=100\,\\mu$F', xlim=[0, 0.5], ylim=[111.5, 113.5], dt = 0, tzero=True, l_win=40, peak_sep=40) #0, 50
plt.savefig('M146_10kHz_100uF.pdf')
plt.show()
#%% Amplitude oscillations vs frequency
matrix = np.array([
    [50, 118, 6, 32, 91, 5, 32],
    [100, 116, 5, 31, 101, 3, 30],
    [150, 113, 3, 37, 102, 3, 37],
    [200, 114, 2, 54, 106, 2, 53],
    [250, 113, 2, 59, 106, 2, 59],
    [300, 115, 1, 35, 109, 1, 34]
])
freq, Vmax_mV,	dVmax_mV, nmax,	Vmin_mV, dVmin_mV, nmin = matrix.T
amplitude = Vmax_mV - Vmin_mV
average = (Vmax_mV + Vmin_mV)/2
damp = np.sqrt(dVmax_mV ** 2 + dVmin_mV ** 2)
fig, ax1 = plt.subplots(figsize=[3,2.5])
plt.tick_params(direction='in', which='both', top=True)
plt.minorticks_on()
ax2 = ax1.twinx()
ax1.errorbar(freq, amplitude, yerr=damp, fmt='ok',ecolor='k', capsize=2)
#ax1.plot(freq, amplitude, 'ko')
ax2.errorbar(freq, average,yerr=damp/2, fmt='ro',ecolor='r', capsize=2)
popt, pcov = curve_fit(lambda freq, a: a / freq, freq, amplitude)
freq_long = np.linspace(50, 300, 200)

ax1.plot(freq_long, popt[0] / freq_long, c='k')
plt.minorticks_on()
ax1.set_ylabel('ripple (mV)', fontsize=20)
ax2.set_ylabel('average (mV)', fontsize=20, color='r')
ax2.tick_params(direction='in', which='both')
#ax2.set_ylim([0, 120])
ax1.set_xlabel('$f$ (Hz)', fontsize=20)
plt.savefig('frequencyDependenceAmplAndRipple.pdf')
plt.show()
plt.figure(figsize=[3,2.5])
plt.plot(freq, nmin,'o',label='min')
plt.plot(freq, nmax,'o',label='max')
plt.xlabel('$f$ (Hz)', fontsize=20)
plt.ylabel('n. points', fontsize=20)
plt.legend()
plt.minorticks_on()
plt.tick_params(direction='in', which='both', top=True, right=True)
plt.savefig('NumberOfPointsRipple.pdf')
plt.show()

#%%
#%%
#%% AC-DC conversion Time dependence f=40 kHz
B0 = 0
Vran = [-0.45, 0.45]
filename1 = 'M149_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.598V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 40160.0 Hz.dat'
filename2 = 'M148_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.598V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCir10uFfreq 40160.0 Hz.dat'
nu1, t1_s, Vsd1_V, Isd1_mA = np.loadtxt(path+filename1, skiprows=1, delimiter=',').T
t1_s = t1_s - np.min(t1_s) -6e-6
nu2, t2_s, Vsd2_V, Isd2_mA = np.loadtxt(path+filename2, skiprows=1, delimiter=',').T
t2_s = t2_s - np.min(t2_s)
plt.figure(figsize=[4, 4])
t0 = 0
plt.subplot(211)

plt.plot(t1_s * 1e6 + t0, savgol_filter(Isd1_mA, 13, 0))
plt.plot(t2_s * 1e6 + t0, savgol_filter(Isd2_mA, 13, 0))
#plt.plot(t3_s * 1e3 + t0, Isd3_mA)
plt.xlim([0, 100])

plt.ylabel('$I_\mathrm{in}$ (mA)', fontsize=20)
plt.minorticks_on()
plt.tick_params(which='both', direction='in', top=True, right=True)
plt.subplot(212)
print('No cap Vavg={} mV'.format(np.mean(Vsd1_V*1e3)))
print('Cap Vavg={} mV'.format(np.mean(Vsd2_V*1e3)))
plt.plot(t1_s * 1e6 + t0, Vsd1_V * 1e3, label='No capacitor')
plt.plot(t2_s * 1e6 + t0, Vsd2_V * 1e3, label='$C=10\,\\mu$F')
#plt.plot(t3_s * 1e3 + t0, Vsd3_V * 1e3, label='$C=100\,\\mu$F')
plt.minorticks_on()
plt.tick_params(which='both', direction='in', top=True, right=True)
plt.xlim([0, 100])
plt.xlabel('$t$ ($\\mu$s)', fontsize=20)
plt.ylabel('$V_\mathrm{out}$ (mV)', fontsize=20)
plt.legend()
plt.savefig('CapacitorEffect40kHz.pdf')
print('Imax={}m A Vnocapmax={} V'.format(max(Isd1_mA), max(Vsd1_V)))
plt.show()
#%%
#%% 100 kHz
fname = 'M126_VPT0112_Ch1_I_V+V-V_I+I-OpenCircuit_Vmax1.772V_1kOhm_Bz-p24Oe__1p7K_1MHzFilt_CapCircuitNoCapfreq 102000.0 Hz.dat'

plot_scope_signal_final(path+fname, title='100kHz', xlim=[], ylim=[], dt = 0, tzero=True,
                         peak_sep=1) #0, 50

plt.show()

